package com.yosaa.back.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yosaa.back.entity.ScoringStandard;
import com.yosaa.back.entity.UserInfo;
import com.yosaa.back.service.IFitnessProjectService;
import com.yosaa.back.service.IScoringStandardService;
import com.yosaa.back.service.IUsersService;
import com.yosaa.back.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 体测评分标准表，存储不同体测项目的评分标准 前端控制器
 * </p>
 *

 */
@RestController
@RequestMapping("/scoring-standard")
public class ScoringStandardController {

    @Autowired
    private IScoringStandardService scoringStandardService;
    @Autowired
    private IUsersService userService;
    @Autowired
    private IUsersService  usersService;
    @GetMapping("/list")
    public ResponseEntity getScoringStandardList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer userName,
            @RequestParam(required = false) String grade) {

        Page<ScoringStandard> standardPage = new Page<>(page, size);

        QueryWrapper<ScoringStandard> queryWrapper = new QueryWrapper<>();
//        if (userName != null) {
//            queryWrapper.eq("project_id", userName);
//        }
        if (grade != null && !grade.trim().isEmpty()) {
            queryWrapper.eq("grade", grade);
        }

        Integer userId = StpUtil.getLoginIdAsInt();
        UserInfo userInfo = usersService.getById(userId);
        if (!userInfo.getRole().equals("admin")) {
            queryWrapper.eq("user_id", userId);
            IPage<ScoringStandard> result = scoringStandardService.page(standardPage, queryWrapper);
            result.getRecords().forEach(standard -> standard.setUserName(userService.getById(standard.getUserId()).getUsername()));
            return ResponseEntity.ok(new ApiResponse(0, result, "操作成功"));
        }

        IPage<ScoringStandard> result = scoringStandardService.page(standardPage, queryWrapper);
        result.getRecords().forEach(standard -> standard.setUserName(userService.getById(standard.getUserId()).getUsername()));
        return ResponseEntity.ok(new ApiResponse(0, result, "操作成功"));
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse> addScoringStandard(@RequestBody ScoringStandard scoringStandard) {
        boolean saved = scoringStandardService.save(scoringStandard);
        if (saved) {
            return ResponseEntity.ok(new ApiResponse(0, null, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse(1, null, "操作失败"));
        }
    }

    @PostMapping("/update")
    public ResponseEntity<ApiResponse> updateScoringStandard(@RequestBody ScoringStandard scoringStandard) {
        boolean updated = scoringStandardService.updateById(scoringStandard);
        if (updated) {
            return ResponseEntity.ok(new ApiResponse(0, null, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse(1, null, "操作失败"));
        }
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<ApiResponse> getScoringStandard(@PathVariable Integer id) {
        ScoringStandard scoringStandard = scoringStandardService.getById(id);
        if (scoringStandard != null) {
            return ResponseEntity.ok(new ApiResponse(0, scoringStandard, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(new ApiResponse(1, null, "操作失败"));
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<ApiResponse> deleteScoringStandard(@PathVariable Integer id) {
        boolean deleted = scoringStandardService.removeById(id);
        if (deleted) {
            return ResponseEntity.ok(new ApiResponse(0, null, "操作成功"));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse(1, null, "操作失败"));
        }
    }
}